﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VASMide
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }
        public string text = "";
        public void redraw()
        {
            if (Form1.vm != null)
            {
                if (Form1.vm.Output != text && !Form1.vm.ExceptInput)
                {
                    text = Form1.vm.Output;

                    this.textBox1.Text = text;
                    oldp = this.textBox1.Text.Length ;
                }
                else
                {

                   
                }
            }
        }
    
        private void timer1_Tick(object sender, EventArgs e)
        {

            if (Form1.vm != null)
            {
                this.Text = "Output Window";
                listBox1.Visible = false;
                if (textBox1.SelectionStart < oldp)
                {
                    textBox1.SelectionStart = oldp;
                }
            }
            else
            {
                this.Text = "Errors";
                listBox1.Visible = true;
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (textBox1.SelectionStart < oldp)
            {
                textBox1.SelectionStart = oldp;
                e.Handled = true;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (Form1.vm != null)
            {
                if (Form1.vm.ExceptInput)
                    text = textBox1.Text;
            }
        }
        int oldp = 0;
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (Form1.vm != null)
            {
                if (e.KeyCode == Keys.Back)
                {
                    if (textBox1.SelectionStart - 1 < oldp)
                    {
                        textBox1.SelectionStart = oldp;
                        e.Handled = true;
                    }
                }
                if (e.KeyCode == Keys.Enter)
                {
                    if (Form1.vm.ExceptInput)
                    {
                        Form1.vm.Output = textBox1.Text;
                        Form1.vm.InputBuffer = this.textBox1.Text.Substring(oldp);
                        Form1.vm.Output += "\n";
                        Form1.vm.ExceptInput = false;
                    }
                }
            }
        }
    }
}
